package com.mall.czlr.base.service.impl;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mall.czlr.base.exception.GlobalException;
import com.mall.czlr.base.service.IBaseService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements IBaseService<T> {

    @Override
    @Transactional
    public T insertReturn(T entity) {
        super.insert(entity);
        return entity;
    }

    @Override
    @Transactional
    public T updateByIdReturn(T entity) throws Exception {
        super.updateById(entity);
        String id = BeanUtils.getProperty(entity, "id");

        if (id == null) {
            throw new GlobalException("主键值为空");
        }

        return super.selectById(id);
    }
}
